package com.property.manage.service.village;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guardchina.framework.mybatis.model.CustomPage;
import com.guardchina.framework.util.exception.BusinessException;
import com.property.manage.dao.village.VillageMapper;
import com.property.manage.model.system.UserDetail;
import com.property.manage.model.village.DownVo;
import com.property.manage.model.village.Village;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;


/**
 * 描述
 *
 * @author lwb
 * @since 2020/7/15
 */
@Service
@Slf4j
public class VillageService {
    @Autowired
    private VillageMapper villageMapper;


    public Village findVillage(Integer id) {
        return villageMapper.selectById(id);
    }

    public CustomPage<Village> villagePage(Long pageNo, Long pageSize, String name) {
        Page<UserDetail> page = new Page<>(pageNo, pageSize);
        IPage<Village> village = villageMapper.getVillage(page, name);
        return new CustomPage<>(village);
    }


    public void add(Integer type, BigDecimal propertyMoney, BigDecimal rent, String name
    ) {
        if (findByName(name)) {
            throw new BusinessException("该小区名称已存在", String.format("name[%s]", name));
        }
        Village village = new Village();
        village.setName(name);
        village.setPropertyMoney(propertyMoney);
        village.setRent(rent);
        village.setType(type);
        villageMapper.insert(village);
    }

    public void update(Integer id,Integer type, BigDecimal propertyMoney,
                       BigDecimal rent,String name) {
        Village village = this.findVillage(id);
        if (village==null){
            throw new BusinessException("id错误", String.format("id[%s]", String.valueOf(id)));
        }
        if (!village.getName().equals(name)){
            if (findByName(name)) {
                throw new BusinessException("小区名称已经存在", String.format("name[%s]", name));
            }
        }
        village.setType(type);
        village.setPropertyMoney(propertyMoney);
        village.setRent(rent);
        village.setName(name);
        villageMapper.updateById(village);
    }

    public void delete(Integer id) {
        villageMapper.deleteById(id);
    }

    public Village getVillage(Integer id){
        Village village = villageMapper.selectById(id);

        return Optional.ofNullable(village).orElseThrow(() -> new BusinessException("小区不存在", String.format("villageId[%s]", id)));
    }

    private boolean findByName(String name) {
        QueryWrapper<Village> wrapper = new QueryWrapper<>();
        wrapper.eq("name", name);
        return villageMapper.selectOne(wrapper) != null;
    }

    public List<DownVo> down() {
        return villageMapper.down();
    }

    public List<Integer> getVillageId(){
        return villageMapper.getVillageId();
    }
}
